#include "mycommon.h"
#include "oled.h"
#include "myi2c.h"
#include "myi2c-dev.h"


static int OLEDfd = -1;

/**********************************************
// IIC Write Command
**********************************************/
void Write_IIC_Command(uint8_t IIC_Command)
{
	i2c_smbus_write_byte_data(OLEDfd,OLED_REG_CMD,IIC_Command);
}

/**********************************************
// IIC Write Data
**********************************************/
void Write_IIC_Data(uint8_t IIC_Data)
{
    i2c_smbus_write_byte_data(OLEDfd, OLED_REG_DAT, IIC_Data);
}

/**********************************************
// IIC Write byte
**********************************************/
void OLED_WR_Byte(unsigned dat, unsigned cmd)
{
	if (cmd){
		Write_IIC_Data(dat);
	}
	else
	{
		Write_IIC_Command(dat);
	}
}

//初始化SSD1306
void OLED_Init(void)
{
    OLEDfd = open(IIC_DEV,O_RDWR);
    if(OLEDfd < 0){
        printf("OPEN I2C DEV failed...\r\n");
    }
    if(setI2CSlave(OLEDfd, OLED_ADD) < 0){
        printf("SET SLAVE failed...\r\n");
    }
	usleep(200*1000);

    OLED_WR_Byte(0xAE,OLED_CMD);//关闭显示

    OLED_WR_Byte(0x40,OLED_CMD);//---set low column address
    OLED_WR_Byte(0xB0,OLED_CMD);//---set high column address

    OLED_WR_Byte(0xC8,OLED_CMD);//-not offset

    OLED_WR_Byte(0x81,OLED_CMD);//设置对比度
    OLED_WR_Byte(0xff,OLED_CMD);

    OLED_WR_Byte(0xa1,OLED_CMD);//段重定向设置

    OLED_WR_Byte(0xa6,OLED_CMD);//

    OLED_WR_Byte(0xa8,OLED_CMD);//设置驱动路数
    OLED_WR_Byte(0x1f,OLED_CMD);

    OLED_WR_Byte(0xd3,OLED_CMD);
    OLED_WR_Byte(0x00,OLED_CMD);

    OLED_WR_Byte(0xd5,OLED_CMD);
    OLED_WR_Byte(0xf0,OLED_CMD);

    OLED_WR_Byte(0xd9,OLED_CMD);
    OLED_WR_Byte(0x22,OLED_CMD);

    OLED_WR_Byte(0xda,OLED_CMD);
    OLED_WR_Byte(0x02,OLED_CMD);

    OLED_WR_Byte(0xdb,OLED_CMD);
    OLED_WR_Byte(0x49,OLED_CMD);

    OLED_WR_Byte(0x8d,OLED_CMD);
    OLED_WR_Byte(0x14,OLED_CMD);

    OLED_WR_Byte(0xaf,OLED_CMD);
}


void OLED_SetPos(unsigned char x, unsigned char y) //设置起始点坐标
{
    OLED_WR_Byte(0xb0+y, OLED_CMD);
    OLED_WR_Byte(((x&0xf0)>>4)|0x10, OLED_CMD);
    OLED_WR_Byte((x&0x0f)|0x01, OLED_CMD);
}

void SetDot(unsigned char x,unsigned char y,int color)//画点函数 【x,y  坐标】【color  :颜色  0：白  1：黑】
{
    int a,b;
    unsigned char c;
    a = y / 8;
    b = y % 8;

    if (color==1){
        c = (0x01<<b);
    }else{
        c = (0x01<<b);
        c = ~c;
    }

    OLED_SetPos(x,a);
    OLED_WR_Byte(c, OLED_DATA);
}

void OLED_Fill(unsigned char fill_Data)//全屏填充
{
    unsigned char m, n;
    for(m = 0; m < 4; m++){
        OLED_WR_Byte(0xb0 + m, OLED_CMD);	//page0-page1
        OLED_WR_Byte(0x00, OLED_CMD);		//low column start address
        OLED_WR_Byte(0x10, OLED_CMD);		//high column start address
        for(n=0;n<128;n++){
            OLED_WR_Byte(fill_Data, OLED_DATA);
        }
    }
}

void OLED_CLS(void)//清屏
{
    OLED_Fill(0x00);
}

void OLED_ON(void)//OLED唤醒
{
    OLED_WR_Byte(0X8D, OLED_CMD);  //设置电荷泵
    OLED_WR_Byte(0X14, OLED_CMD);  //开启电荷泵
    OLED_WR_Byte(0XAF, OLED_CMD);  //OLED唤醒
}

void OLED_OFF(void)//让OLED休眠
{
    OLED_WR_Byte(0X8D, OLED_CMD);  //设置电荷泵
    OLED_WR_Byte(0X10, OLED_CMD);  //关闭电荷泵
    OLED_WR_Byte(0XAE, OLED_CMD);  //OLED休眠
    close(OLEDfd);
}


//关闭oled文件
int OLED_Close(void)
{
	close(OLEDfd);
    return 0;
}
